/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginPage.java
 *
 * Created on Oct 20, 2010, 10:10:02 PM
 */

//PACKAGE NAME
package matchmakingsystem;

//LIB FOR ORACLE
import java.sql.*;

//LIB FOR JOPTIONPANE
import javax.swing.JOptionPane;

/**
 *
 * @author shirni
 */
public class LoginPage extends javax.swing.JFrame {

    //A public static string variable to pass the staff name to other module
    public static  String getUsername;
    /** Creates new form LoginPage */
    public LoginPage()
    {
        initComponents();

        changePasswordPanel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        changePasswordPanel = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPasswordField2 = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        jPasswordField3 = new javax.swing.JPasswordField();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(java.awt.Color.white);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Purisa", 1, 48));
        jLabel1.setText("MATCH-MAKING SYSTEM");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 580, -1, -1));

        jLabel2.setFont(new java.awt.Font("Purisa", 1, 48));
        jLabel2.setText("WELCOME");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 390, -1, -1));

        jLabel3.setFont(new java.awt.Font("Purisa", 1, 48)); // NOI18N
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/heart13.gif"))); // NOI18N
        jLabel3.setText("TO");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 470, -1, -1));

        jPanel1.setBackground(java.awt.Color.white);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(null, "USER LOGIN", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Purisa", 1, 36)), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Purisa", 1, 36))); // NOI18N
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel5.setFont(new java.awt.Font("Purisa", 1, 14));
        jLabel5.setText("USERNAME:");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 80, -1, -1));

        jLabel4.setFont(new java.awt.Font("Purisa", 1, 14));
        jLabel4.setText("PASSWORD:");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 120, -1, -1));
        jPanel1.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 80, 180, -1));
        jPanel1.add(jPasswordField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 120, 180, -1));

        jButton1.setBackground(java.awt.Color.white);
        jButton1.setFont(new java.awt.Font("Purisa", 1, 14)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/unlock.gif"))); // NOI18N
        jButton1.setText("LOGIN");
        jButton1.setBorder(null);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 170, -1, -1));

        jButton2.setBackground(java.awt.Color.white);
        jButton2.setFont(new java.awt.Font("Purisa", 1, 14)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/erase.gif"))); // NOI18N
        jButton2.setText("CANCEL");
        jButton2.setBorder(null);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 170, -1, -1));

        jButton3.setBackground(new java.awt.Color(255, 255, 255));
        jButton3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jButton3.setForeground(new java.awt.Color(0, 204, 255));
        jButton3.setText("CHANGE PASSWORD");
        jButton3.setBorder(null);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 140, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 70, 490, 260));

        changePasswordPanel.setBackground(new java.awt.Color(255, 255, 255));
        changePasswordPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        changePasswordPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setText("NEW PASSWORD:");
        changePasswordPanel.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 50, -1, 20));
        changePasswordPanel.add(jPasswordField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 110, 130, -1));

        jLabel7.setText("COMFIRM PASSWORD:");
        changePasswordPanel.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 110, -1, 20));
        changePasswordPanel.add(jPasswordField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 50, 130, -1));

        jButton4.setBackground(java.awt.Color.white);
        jButton4.setFont(new java.awt.Font("Purisa", 1, 14)); // NOI18N
        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/blue-floppy.gif"))); // NOI18N
        jButton4.setText("SAVE");
        jButton4.setBorder(null);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        changePasswordPanel.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 170, 80, -1));

        jButton5.setBackground(java.awt.Color.white);
        jButton5.setFont(new java.awt.Font("Purisa", 1, 14)); // NOI18N
        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/erase.gif"))); // NOI18N
        jButton5.setText("CANCEL");
        jButton5.setBorder(null);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        changePasswordPanel.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 170, 100, -1));

        getContentPane().add(changePasswordPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 130, 350, 240));

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/heart1.jpg"))); // NOI18N
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

//***************************** LOGIN PAGE *******************************************************
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        String username = jTextField1.getText().trim();
        String password  = jPasswordField1.getText();
        getUsername  = username;
        String SQL = "SELECT * FROM EMP_PASSWORD WHERE EMP_CODE = '" + username + "'";

        String checkUsername = null;
        String checkPassword = null;

        try {
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            Connection con;
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","mms","mms");
            System.out.println("DATABASE CONNECTED");

            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery(SQL);

            while(rset.next()) {
                checkUsername = rset.getString("EMP_CODE");
                checkPassword = rset.getString("EMP_PW");
            }

            if(!checkPassword.equals(password)) {
                JOptionPane.showMessageDialog(null, "User does not exist or incorrect password. \nMake sure you had entered \na correct username and password.", "ERROR!", JOptionPane.WARNING_MESSAGE);
                jTextField1.setText("");
                jPasswordField1.setText("");
            }

            else if(checkPassword.equals(password)) {
                new MainMenu().setVisible(true);
                setVisible(false);
            }
        }

        catch (Exception e) {
                System.out.println("CONNNECT ERROR");
                JOptionPane.showMessageDialog(null, "User does not exist or incorrect password. \nMake sure you had entered \na correct username and password.", "ERROR!", JOptionPane.WARNING_MESSAGE);
                jTextField1.setText("");
                jPasswordField1.setText("");
        }
    }//GEN-LAST:event_jButton1ActionPerformed
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*~*~



//***************************** CANCEL BUTTON ***********************************************
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        jTextField1.setText("");
        jPasswordField1.setText("");
    }//GEN-LAST:event_jButton2ActionPerformed
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*



//***************************** CHANGING USER PASSWORD ***************************************
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        String username = jTextField1.getText().trim();
        String password  = jPasswordField1.getText();

        String SQL = "SELECT * FROM EMP_PASSWORD WHERE EMP_CODE = '" + username + "'";

        String checkUsername = null;
        String checkPassword = null;

        try {
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            Connection con;
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","mms","mms");
            System.out.println("DATABASE CONNECTED");

            Statement stmt = con.createStatement();
            ResultSet rset = stmt.executeQuery(SQL);

            while(rset.next()) {
                checkUsername = rset.getString("EMP_CODE");
                checkPassword = rset.getString("EMP_PW");
            }

            if(!checkPassword.equals(password)) {
                JOptionPane.showMessageDialog(null, "User does not exist or incorrect password. \nMake sure you had entered \na correct username and password.", "ERROR!", JOptionPane.WARNING_MESSAGE);
                jTextField1.setText("");
                jPasswordField1.setText("");
            }

            else if(checkPassword.equals(password)) {
                jPanel1.setVisible(false);
                changePasswordPanel.setVisible(true);
                jPasswordField1.setText("");
            }
        }

        catch (Exception e) {
                System.out.println("CONNNECT ERROR");
                JOptionPane.showMessageDialog(null, "User does not exist or incorrect password. \nMake sure you had entered \na correct username and password.", "ERROR!", JOptionPane.WARNING_MESSAGE);
                jTextField1.setText("");
                jPasswordField1.setText("");
        }
    }//GEN-LAST:event_jButton3ActionPerformed
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*



//***************************** SAVING PASSWORD AFTER CHANGING *******************************
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        String username = jTextField1.getText().trim();
        String newpw = jPasswordField3.getText();
        String comfirmpw = jPasswordField2.getText();
        String SQL = "UPDATE EMP_PASSWORD SET EMP_PW = '" + newpw + "' WHERE EMP_CODE = '" + username + "'";

        if(!comfirmpw.equals(newpw)) {
            jPasswordField2.setText("");
            JOptionPane.showMessageDialog(null, "Recomfirm password.", "ERROR!", JOptionPane.WARNING_MESSAGE);
        }//end if

        else if(comfirmpw.equals(newpw)) {

            try {
                DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
                Connection con;
                con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","mms","mms");
                System.out.println("DATABASE CONNECTED");

                Statement stmt = con.createStatement();
                ResultSet rset = stmt.executeQuery(SQL);

                JOptionPane.showMessageDialog(null, "You may now log in with your new password.", "UPDATE SUCCESSFUL!", JOptionPane.WARNING_MESSAGE);
                changePasswordPanel.setVisible(false);
                jPanel1.setVisible(true);
                jPasswordField3.setText("");
                jPasswordField2.setText("");

            }//end try

            catch (Exception e) {
                    System.out.println("CONNNECT ERROR");
                    JOptionPane.showMessageDialog(null, "User does not exist or incorrect password. \nMake sure you had entered \na correct username and password.", "ERROR!", JOptionPane.WARNING_MESSAGE);
                    jTextField1.setText("");
                    jPasswordField1.setText("");
            }//end catch
        }//end else if

    }//GEN-LAST:event_jButton4ActionPerformed
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*



//***************************** CANCEL PASSWORD CHANGING *************************************
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        changePasswordPanel.setVisible(false);
        jPanel1.setVisible(true);
        jPasswordField3.setText("");
        jPasswordField2.setText("");
    }//GEN-LAST:event_jButton5ActionPerformed
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginPage().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel changePasswordPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JPasswordField jPasswordField3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
